package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ApUserInfoMapper;
import com.zhentao.mapper.ApUserMapper;
import com.zhentao.model.activity.vo.ActivityVo;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.realname.dto.RealnameDto;
import com.zhentao.model.realname.pojo.ApRealname;
import com.zhentao.model.user.pojo.ApUser;
import com.zhentao.model.user.pojo.ApUserInfo;
import com.zhentao.service.ApRealnameService;
import com.zhentao.mapper.ApRealnameMapper;
import com.zhentao.utils.RealName;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author 许文倩
* @description 针对表【ap_realname】的数据库操作Service实现
* @createDate 2024-07-01 21:13:12
*/
@Service
public class ApRealnameServiceImpl extends ServiceImpl<ApRealnameMapper, ApRealname>
    implements ApRealnameService{
    @Resource
    ApRealnameMapper apRealnameMapper;
    @Resource
    ApUserMapper apUserMapper;
    @Resource
    ApUserInfoMapper apUserInfoMapper;

    @Override
    public ResponseResult getlist(RealnameDto dto) {
        System.out.println(dto.getPageNum()+"-------------------"+dto.getPageSize()+"");
        Page<ApRealname> page=new Page(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<ApRealname> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(dto.getRealnameName()!=null,ApRealname::getRealnameName,dto.getRealnameName());
        page = apRealnameMapper.selectPage(page,wrapper);
        System.err.println(page.getTotal());
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPageNum(), dto.getPageSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        pageResponseResult.setCode(200);
        pageResponseResult.setErrorMessage("查询成功");
        return pageResponseResult;
    }

    @Override
    public ResponseResult add(ApRealname realname){
        ResponseResult responseResult = RealName.autoCheck(realname.getIdno(), realname.getRealnameName());
        if (responseResult.getCode()==200){
            QueryWrapper<ApRealname> wrapper=new QueryWrapper<>();
            wrapper.lambda().eq(ApRealname::getIdno,realname.getIdno());
            ApRealname apRealname = apRealnameMapper.selectOne(wrapper);
            if (apRealname!=null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
            Map<String,Object> map =(Map<String, Object>) responseResult.getData();
            //验证成功，添加到实名认证库
            realname.setGender((Integer) map.get("gender"));
            realname.setNativePlace((String)map.get("nativePlace"));
            realname.setBirthdate((Date)map.get("birthdate"));
            realname.setAuditStatus(1);
            realname.setPassTime(new Date());
            realname.setCreatedTime(new Date());
            apRealnameMapper.insert(realname);
            //修改状态
//            ApUser apUser=new ApUser();
            QueryWrapper<ApUser> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(ApUser::getId,realname.getUserId());
            ApUser apUser = apUserMapper.selectOne(queryWrapper);
            apUser.setIsReal(1);
            apUserMapper.updateById(apUser);
            ApUserInfo apUserInfo=new ApUserInfo();
            apUserInfo.setUserId(realname.getUserId());
            apUserInfo.setUserId(realname.getUserId());
            //户籍地
            apUserInfo.setNativePlace(realname.getNativePlace());
            //姓名
            apUserInfo.setRealnameName(realname.getRealnameName());
            //生日
            apUserInfo.setBirthdate(realname.getBirthdate());
            //性别
            apUserInfo.setGender(realname.getGender());
            //审核状态
            apUserInfo.setAuditStatus(0);
            apUserInfo.setCreatedTime(new Date());
            apUserInfoMapper.insert(apUserInfo);
        }
        return responseResult;
    }

    @Override
    public ResponseResult findAllAcByUser(Integer id) {
        List<ActivityVo> activityVos= apRealnameMapper.findAllAcByUser(id);
        return ResponseResult.okResult(activityVos);
    }

    @Override
    public ResponseResult getByUserId(Integer userId) {
        QueryWrapper<ApRealname> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(ApRealname::getUserId,userId);
        ApRealname apRealname = apRealnameMapper.selectOne(wrapper);
        if (apRealname==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(apRealname);
    }
}




